package com.niit.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;

@Component
public class SendMsg {
    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;
    private String msg;
    private String title;

    public String makeCode(){
        Random random = new Random();
        String code = "";
        for(int i = 0;i<4;i++){
            code+= random.nextInt(10);
        }
        return code;
    }

    public String makeMsg(String code){
        msg = "您的验证码是："+code+", 请在十五分钟内完成登录!";
        return msg;
    }
    public String makeTitle(){
        title = "galgal 验证码";
        return title;
    }

    public void sendMsg(String to,String subject,String content) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        message.setFrom(from);
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setText(content);
        javaMailSender.send(mimeMessageHelper.getMimeMessage());
    }






}
